package ntd.util.wx;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import ntd.util.UtilLocal;
import ntd.util.wx.vo.ResVO;
import org.w3c.dom.Document;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/**
 * 微信支付相关工具类
 */
public class UtilWxApplets {

    final private static String URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // 统一下单接口地址
    final private static String appid = "wx1ea5ef5e3047d3f0"; // appid
    final private static String secret = "4c34d1b912d3ad8d360bbfce312da28b"; // appid
    final private static String mch_id = "1603454478"; // 商户号
    final private static String key = "Sf7ydXEWMCmBuCEOG3QI8EHV8pfZ5HAq"; // 商户密钥 - 签名使用
    final private static String notify_url = "https://jiuzhif.com/admin/pay/settlementSuccess"; // 商户密钥 - 签名使用

    public static ResVO wxLogin(String js_code) { // 小程序
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        final Map<String, Object> params = new TreeMap();
        params.put("appid", appid);
        params.put("secret", secret);
        params.put("js_code", js_code);
        params.put("grant_type", "authorization_code");
        final String res = HttpUtil.get(url, params);
        return JSONUtil.toBean(res, ResVO.class);
    }

    /**
     * 统一下单接口
     *
     * @param device_info
     * @param out_trade_no
     * @param total_fee
     * @param openid
     * @return
     */
    public static String unifiedorder(String device_info, String out_trade_no, String total_fee, String openid) {
        final Map<String, Object> params = new TreeMap();
        params.put("appid", appid);
        params.put("mch_id", mch_id);
        params.put("nonce_str", RandomUtil.randomStringUpper(32));
        if (StrUtil.isNotBlank(device_info)) {
            params.put("device_info", device_info);
        }
        params.put("body", "宋词-酒智付");
        params.put("out_trade_no", out_trade_no);
        params.put("total_fee", total_fee);
        params.put("notify_url", notify_url);
        params.put("trade_type", "JSAPI");
        params.put("openid", openid);
        params.put("spbill_create_ip", UtilLocal.getWebIp());
        params.put("sign", UtilWx.paySign(params, key));
        final Document document = XmlUtil.mapToXml(params, "xml");
        final String post = HttpUtil.post(URL, XmlUtil.toStr(document));
        final Map<String, Object> map = XmlUtil.xmlToMap(post);
        if (map.get("return_code").equals("SUCCESS") && map.get("result_code").equals("SUCCESS")) {
            return String.valueOf(map.get("prepay_id"));
        }
        return null;
    }

    public static void main(String[] args) {
//        final String unifiedorder = unifiedorder(null, "123456789", "88", "");
//        Console.log(unifiedorder);
    }

    /**
     * 获取小程序支付请求参数
     *
     * @param prepay_id
     * @return
     */
    public static Map<String, Object> requestPayment(String prepay_id) {
        final Map<String, Object> params = new TreeMap();
        params.put("appId", appid);
        params.put("timeStamp", new Date().getTime());
        params.put("nonceStr", RandomUtil.randomStringUpper(32));
        params.put("package", prepay_id);
        params.put("signType", "MD5");
        params.put("sign", UtilWx.paySign(params, key));
        return params;
    }
}
